package com.aphrodite.io.sip.header;

import com.aphrodite.util.StringParser;

/** Subscription-State header (see RFC3265 for details). */
public final class SubscriptionStateHeader extends ParametricHeader {
	/** State "active" */
	public static final String ACTIVE = "active";

	/** State delimiters. */
	private static final char[] delim = { ',', ';', ' ', '\t', '\n', '\r' };

	/** State "pending" */
	public static final String PENDING = "pending";

	/** State "terminated" */
	public static final String TERMINATED = "terminated";

	/** Costructs a new SubscriptionStateHeader. */
	public SubscriptionStateHeader(Header hd) {
		super(hd);
	}

	/** Costructs a new SubscriptionStateHeader. */
	public SubscriptionStateHeader(String state) {
		super(SipHeaders.Subscription_State, state);
	}

	/** Gets the 'expires' param. */
	public int getExpires() {
		String exp = getParameter("expires");
		if (exp != null)
			return Integer.parseInt(exp);
		else
			return -1;
	}

	/** Gets the 'reason' param. */
	public String getReason() {
		return getParameter("reason");
	}

	/** Gets the subscription state. */
	public String getState() {
		return new StringParser(value).getWord(delim);
	}

	/** Whether there is the 'expires' param. */
	public boolean hasExpires() {
		return hasParameter("expires");
	}

	/** Whether there is the 'reason' param. */
	public boolean hasReason() {
		return hasParameter("reason");
	}

	/** Whether the subscription is active. */
	public boolean isActive() {
		return getState().equals(ACTIVE);
	}

	/** Whether the subscription is pending. */
	public boolean isPending() {
		return getState().equals(PENDING);
	}

	/** Whether the subscription is terminated. */
	public boolean isTerminated() {
		return getState().equals(TERMINATED);
	}

	/** Sets the 'expires' param. */
	public SubscriptionStateHeader setExpires(int secs) {
		setParameter("expires", Integer.toString(secs));
		return this;
	}

	/** Sets the 'reason' param. */
	public SubscriptionStateHeader setReason(String reason) {
		setParameter("reason", reason);
		return this;
	}

}
